// 模拟用户数据
const users = [
    {
      id: 1,
      name: "管理员",
      email: "admin@example.com",
      password: "admin123",
      role: "admin",
      address: "北京市海淀区中关村大街1号",
      phone: "13800138000",
      avatar: "https://picsum.photos/id/1005/200/200"
    },
    {
      id: 2,
      name: "张三",
      email: "zhangsan@example.com",
      password: "123456",
      role: "user",
      address: "上海市浦东新区张江高科技园区",
      phone: "13900139000",
      avatar: "https://picsum.photos/id/1012/200/200"
    }
  ];
  
  // 用户服务类
  class UserService {
    // 登录方法
    login(email, password) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          const user = users.find(u => u.email === email && u.password === password);
          if (user) {
            resolve(user);
          } else {
            reject(new Error('用户名或密码错误'));
          }
        }, 500);
      });
    }
    
    // 注册方法
    register(userData) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          // 检查邮箱是否已存在
          const existingUser = users.find(u => u.email === userData.email);
          if (existingUser) {
            reject(new Error('该邮箱已被注册'));
            return;
          }
          
          // 创建新用户
          const newUser = {
            id: users.length + 1,
            ...userData,
            role: 'user',
            avatar: `https://picsum.photos/id/${users.length + 100}/200/200`
          };
          
          users.push(newUser);
          resolve(newUser);
        }, 800);
      });
    }
    
    // 获取用户信息
    getUserById(id) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          const user = users.find(u => u.id === id);
          if (user) {
            resolve(user);
          } else {
            reject(new Error('用户不存在'));
          }
        }, 300);
      });
    }
    
    // 更新用户信息
    updateUser(userId, userData) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          const userIndex = users.findIndex(u => u.id === userId);
          if (userIndex === -1) {
            reject(new Error('用户不存在'));
            return;
          }
          
          // 更新用户信息
          users[userIndex] = {
            ...users[userIndex],
            ...userData
          };
          
          resolve(users[userIndex]);
        }, 500);
      });
    }
  }
  
  export default new UserService();
  